<template>
  <div class="city-container">
    <div ref="scrollWrapper" class="wrapper">
      <div>
        <section id="city-list" class="city-list-container" style="display: block;">
          <section>
            <div id="locate" class="city-title">
              定位城市
            </div>
            <div class="city-list city-list-inline clearfix">
              <div class="location-city">定位失败，请点击重试</div>
            </div>
          </section>

          <section class="history-city-list">
            <div id="history" class="city-title">
              最近访问城市
            </div>
            <div class="city-list city-list-inline clearfix">

              <div class="city-item" v-for="c in historyCities " :key="c.id">
                {{c.name}}
              </div>

            </div>
          </section>

          <section>
            <div id="hot" class="city-title">
              热门城市
            </div>
            <div class="city-list city-list-inline clearfix">

              <div class="city-item" v-for="c in hotCities" @click="setCity(c)" :key="c.id">
                {{c.name}}
              </div>

            </div>
          </section>

          <section>

            <div v-for="(citylist , name ) in cities " :key="name">
              <div :id="name" class="city-title city-title-letter">
                {{name}}
              </div>
              <div class="city-list city-list-block clearfix">
                <div class="city-item" @click="setCity(city)" v-for="city in citylist" :key="city.id" data-nm="阿拉善盟" data-id="150">
                  {{city.name}}
                </div>

              </div>
            </div>
          </section>

        </section>

      </div>
    </div>
    <section class="nav">
      <div class="  nav-item" data-id="locate" @click="setPosition('locate')">
        定位
      </div>

      <div class="  nav-item" data-id="history" @click="setPosition('history')">
        最近
      </div>

      <div class="  nav-item" data-id="hot" @click="setPosition('hot')">
        热门
      </div>

      <div class="nav-letter  nav-item" v-for="l in getLetters" @click="setPosition(l)" :ref="l" @touchmove="handleTouch" :key="l" data-id="A">
        {{l}}
      </div>
    </section>
  </div>
</template>

<script>
import BScroll from "@better-scroll/core"
import { getCities } from "@/api/movie"
import Pullup from "@better-scroll/pull-up"
BScroll.use(Pullup)

export default {
  data() {
    return {
      historyCities: [],
      hotCities: [],
      cities: [],
      topHeight: 0,
      baseLetterHeight: 0
    }
  },
  computed: {
    getLetters() {
      return Object.keys(this.cities)
    }
  },
  methods: {
    setCity(city) {
      this.$cache.addCity(city)
      this.$store.commit("SET_CITY", city)
      this.$router.go(-1)
    },
    setPosition(letter) {
      this.scroll.scrollToElement(`#${letter}`, 200)
    },
    initScroll() {
      this.scroll = new BScroll(this.$refs.scrollWrapper, {
        click: true,
        scrollY: true,
        probeType: 3,
        pullUpLoad: true
      })
    },
    handleTouch(event) {
      console.log(event.touches[0].pageY)
      let currentY = event.touches[0].pageY
      let index =Math.floor((currentY - this.topHeight) / this.baseLetterHeight)
      let letter = Object.keys(this.cities)[index]
      this.scroll.scrollToElement(`#${letter}`, 100)
    }
  },
  async created() {
    let rs = await getCities()
    this.hotCities = rs.data.hotCities
    this.cities = rs.data.cities
    this.$nextTick(() => {
      this.initScroll()
      //字母A距离页面顶部的高度
      this.topHeight = this.$refs["A"][0].getBoundingClientRect().y
      //每个字符所占的高度
      this.baseLetterHeight = this.$refs["A"][0].offsetHeight
    })
  },
  mounted() {
    let list = JSON.parse(localStorage.getItem("historyCities"))
    let start = list.length > 6 ? list.length - 6 : 0
    this.historyCities = list.slice(start).reverse()
  }
}
</script>



<style lang="scss" scoped>
.city-container {
  height: 100%;
}
.wrapper {
  height: 100%;
  overflow: hidden;
}
.city-entry-arrow {
  width: 0;
  height: 0;
  border: 4px solid #b0b0b0;
  border-left-color: transparent;
  border-right-color: transparent;
  border-bottom-color: transparent;
  display: inline-block;
  margin-left: 4px;
  margin-right: 5px;
  margin-top: 5px;
}

.city-list-container {
  display: none;
  background-color: #ebebeb;
  font-size: 14px;
  color: #333;
  /* top: 0; */
  /* bottom: 0; */
  height: 100%;
  width: 100%;
  /* position: absolute;
  z-index: 1000; */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.clearfix:after {
  content: " ";
  display: table;
  clear: both;
}

.city-title {
  padding-left: 15px;
  line-height: 30px;
}

.city-title-letter {
  padding-left: 25px;
}

.city-list {
  padding-right: 30px;
}

.city-list-inline {
  background-color: #f5f5f5;
  padding-bottom: 8px;
}

.city-list-inline .city-item,
.city-list-inline .location-city {
  float: left;
  background: #fff;
  width: 29%;
  height: 33px;
  margin-top: 15px;
  margin-left: 4%;
  padding: 0 4px;
  border: 1px solid #e6e6e6;
  border-radius: 3px;
  line-height: 33px;
  text-align: center;
  box-sizing: border-box;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.city-list-inline .location-city {
  width: auto;
  min-width: 30%;
  padding: 0 20px;
}

.city-list-block {
  background-color: #f5f5f5;
}

.city-list-block .city-item {
  height: 44px;
  line-height: 44px;
  margin-left: 15px;
  border-bottom: 1px solid #c8c7cc;
}

.city-list-block .city-item:last-child {
  border-bottom: none;
}

.nav {
  position: fixed;
  top: 75px;
  top: 11vh;
  right: 0;
  width: 35px;
  z-index: 10;
  text-align: center;
  font-size: 12px;
}

.nav .nav-item {
  height: 16px;
  height: 2.8vh;
}

.nav .nav-letter {
  width: 15px;
  margin-left: 15px;
}

.city-entry {
  padding-left: 15px;
  font-size: 15px;
  color: #666;
  display: -webkit-box;
  display: flex;
  -webkit-box-align: center;
  align-items: center;
}

.city-entry-arrow {
  width: 0;
  height: 0;
  border: 4px solid #b0b0b0;
  border-left-color: transparent;
  border-right-color: transparent;
  border-bottom-color: transparent;
  display: inline-block;
  margin-left: 4px;
  margin-top: 5px;
}
</style>

